import { ref, reactive } from 'vue'
import { message } from 'ant-design-vue'
import Api from '@/api/user.js'

export const useUserForm = () => {
  const options1 = ref([
    { label: '舟山交警支队', value: '1' },
    { label: '定海交警大队', value: '2' },
  ])

  const options2 = ref([
    { label: '管理员', value: '1' },
    { label: '普通用户', value: '2' },
  ])
  const formRef = ref()
  const formState = reactive<any>({
    id: '',
    name: '',
    realName: '',
    password: '',
    passwordAgain: '',
    subordinate: undefined as any,
    role: [] as any,
    roleName: '',
    orgName: '',
    level: undefined,
  })

  const passwordValid = async (_rule: any, value: string) => {
    if (!value) {
      return Promise.reject('请输入密码')
    }
    if (!/^[a-zA-Z0-9]{8,16}$/.test(value)) {
      return Promise.reject('数字 + 字母(强制区分大小写),长度8-16位')
    }
    return Promise.resolve()
  }

  const passwordValid3 = async (_rule: any, value: string) => {
    if (!value) {
      return Promise.reject('请再次输入密码')
    }
    if (!/^[a-zA-Z0-9]{8,16}$/.test(value)) {
      return Promise.reject('数字 + 字母(强制区分大小写),长度8-16位')
    }
    if (formState.password !== value) {
      return Promise.reject('输入密码不一致')
    }
    return Promise.resolve()
  }

  const passwordValid2 = async (_rule: any, value: string) => {
    if (!value) {
      return Promise.reject('请输入用户名称')
    }
    if (!/^[a-zA-Z0-9]{8,16}$/.test(value)) {
      return Promise.reject('数字 + 字母(强制区分大小写),长度8-16位')
    }
    return Promise.resolve()
  }

  const rules = {
    name: [
      {
        required: true,
        validator: passwordValid2,
        trigger: 'change',
      },
      {
        min: 1,
        max: 16,
        message: '超过最大16字符',
        trigger: 'blur',
      },
    ],
    realName: [
      {
        required: true,
        message: '请输入组织名称',
        trigger: 'change',
      },
      {
        min: 1,
        max: 16,
        message: '超过最大16字符',
        trigger: 'blur',
      },
    ],
    password: [
      {
        required: true,
        validator: passwordValid,
        trigger: 'change',
      },
    ],
    passwordAgain: [
      {
        required: true,
        validator: passwordValid3,
        trigger: 'change',
      },
    ],
    subordinate: [
      {
        required: true,
        message: '请选择所属组织',
        trigger: 'change',
      },
    ],
    role: [
      {
        required: true,
        message: '请选择角色名称',
        trigger: 'change',
      },
    ],
  }

  const resetForm = () => {
    formRef.value?.resetFields()
    Object.assign(formState, {
      id: '',
      name: '',
      realName: '',
      password: '',
      passwordAgain: '',
      subordinate: undefined,
      role: [],
      roleName: '',
      orgName: '',
      level: undefined,
    })
  }

  const submitForm = async () => {
    try {
      await formRef.value.validate()
      const params = {
        policeId: formState.id,
        nickName: formState.name,
        policeName: formState.realName,
        deptCode: formState.subordinate,
        password: formState.password,
        deptName: options1.value.find((e: any) => e.value == formState.subordinate)?.label,
        roleInfo: options2.value
          .filter((e: any) => formState.role.includes(e.value))
          .map((e) => ({
            roleId: e.value,
            roleName: e.label,
          })),
      }
      if (formState.id) {
        await Api.updata(params)
        message.success('修改成功')
      } else {
        await Api.add(params)
        message.success('新增成功')
      }
      return true
    } catch (error) {
      console.error('表单提交错误:', error)
      return false
    }
  }

  return {
    formRef,
    formState,
    rules,
    resetForm,
    submitForm,
    options1,
    options2,
  }
}
